﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using FukiERP.Common.Constants;
using FukiERP.Helper;
using FukiERP.Services.DBContext;
using FukiERP.Services.Helper;

namespace FukiERP.Supplier
{
    public partial class frm_Supplier : Telerik.WinControls.UI.RadForm
    {
        int _currentID = 0;
        public frm_Supplier()
        {
            InitializeComponent();
        }

        private void Supplier_Load(object sender, EventArgs e) {
            LoadData();
        }
        private void LoadData() {
            FukiERPDataContext context = new FukiERPDataContext();
            List<Services.DBContext.Supplier> suppliers = context.Suppliers.OrderBy(c => c.Name).ToList();
            grd_SupplierList.DataSource = suppliers;
        }

        private void RefreshForm() {
            _currentID = 0;
            PresentHelper.ClearTextBox(tbx_SupplierName, tbx_Address, tbx_ContactPerson,
                tbx_Email, tbx_MainPhone, tbx_SubPhone, tbx_SupplierDes, tbx_BankAccount, tbx_TaxNumber);
        }

        private void btn_Create_Click(object sender, EventArgs e) {
            RefreshForm();
        }

        private void btn_Delete_Click(object sender, EventArgs e) {
            if (_currentID != 0 && PresentHelper.ShowConfirmBox(MessageConstants.ConfirmDeleteMessage)) {
                FukiERPDataContext context = DataAccess.GetDBContext();
                Services.DBContext.Supplier supplier = context.Suppliers.Where(c => c.ID == _currentID).FirstOrDefault();
                if (supplier != null) {
                    context.Suppliers.DeleteOnSubmit(supplier);
                    context.SubmitChanges();
                }
                MessageBox.Show(MessageConstants.DeleteSuccessMessage);
                RefreshForm();
                LoadData();
            }
        }

        private void tbn_Save_Click(object sender, EventArgs e) {
            errorProvider1.Clear();
            bool isValid = ValidateValue.ValidateRequirementGroup(errorProvider1, tbx_SupplierName, 
                tbx_Address, tbx_Email, tbx_MainPhone, tbx_ContactPerson);
            if (!isValid) {
                MessageBox.Show(MessageConstants.GaneralValidateMessage);
                return;
            }
            FukiERPDataContext context = DataAccess.GetDBContext();
            Services.DBContext.Supplier supplier = null;
            if (_currentID != 0) {
                supplier = context.Suppliers.Where(c => c.ID == _currentID).FirstOrDefault();
            }
            if (supplier == null) {
                supplier = new Services.DBContext.Supplier();
                context.Suppliers.InsertOnSubmit(supplier);
            }
            supplier.Name = tbx_SupplierName.Text;
            supplier.Description = tbx_SupplierDes.Text;
            supplier.ContactName = tbx_ContactPerson.Text;
            supplier.Address = tbx_Address.Text;
            supplier.MainPhone = tbx_MainPhone.Text;
            supplier.SubPhone = tbx_SubPhone.Text;
            supplier.Email = tbx_Email.Text;
            supplier.TaxID = tbx_TaxNumber.Text;
            supplier.BankAccount = tbx_BankAccount.Text;
            context.SubmitChanges();
            MessageBox.Show(MessageConstants.UpdateSuccessMessage);
            RefreshForm();
            LoadData();
        }

        private void grd_SupplierList_CellDoubleClick(object sender, GridViewCellEventArgs e) {
            _currentID = int.Parse(grd_SupplierList.Rows[e.RowIndex].Cells[0].Value.ToString());
            Services.DBContext.Supplier supplier = (Services.DBContext.Supplier)grd_SupplierList.Rows[e.RowIndex].DataBoundItem;
            tbx_SupplierName.Text = supplier.Name;
            tbx_Address.Text = supplier.Address;
            tbx_ContactPerson.Text = supplier.ContactName;
            tbx_MainPhone.Text = supplier.MainPhone;
            tbx_SubPhone.Text = supplier.SubPhone;
            tbx_Email.Text = supplier.Email;
            tbx_BankAccount.Text = supplier.BankAccount;
            tbx_TaxNumber.Text = supplier.TaxID;
            tbx_SupplierDes.Text = supplier.Description;
        }
    }
}
